import UIBase from './UIBase';
const { ccclass, property } = cc._decorator;
//  none,
//     fade,
//     scale,
//     scaleX,
//     scaleY,
//     topIn,
//     bottomIn,
//     leftIn,
//     rightIn
//专门处理动画切换
@ccclass
export class UIAnimation {
    private _ui: UIBase = null;
    private _viewSize: cc.Size = cc.view.getDesignResolutionSize();
    // 添加构造函数接收UIBase实例
    // constructor(ui: UIBase) {
    //     this._ui = ui; // 初始化_ui属性
    // }
    private _objCallback = {
        'open': [
            (cb: Function) => {
                cb();
            },
            (cb: Function) => {
                // cc.Tween.stopAllByTarget(this._ui.node);
                this._ui.node.opacity = 0;
                cc.tween(this._ui.node)
                    .to(0.5, { opacity: 255 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scale = 0;
                cc.tween(this._ui.node)
                    .to(0.5, { scale: 1 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scaleX = 0
                cc.tween(this._ui.node)
                    .to(0.5, { scaleX: 1 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scaleY = 0
                cc.tween(this._ui.node)
                    .to(0.5, { scaleY: 1 })
                    .call(() => {
                        cb.bind(this);
                    })
                    .start();
            },
            (cb: Function) => {
                let pos = this._ui.node.position.clone();
                this._ui.node.setPosition(cc.v3(this._ui.node.position.x, this._viewSize.height, 0));
                cc.tween(this._ui.node)
                    .to(0.5, { position: pos })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                let pos = this._ui.node.position.clone();
                this._ui.node.setPosition(cc.v3(this._ui.node.position.x, -this._viewSize.height, 0));
                cc.tween(this._ui.node)
                    .to(0.5, { position: pos })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                let pos = this._ui.node.position.clone();
                this._ui.node.setPosition(cc.v3(-this._viewSize.width, this._ui.node.position.y, 0));
                cc.tween(this._ui.node)
                    .to(0.5, { position: pos })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.setPosition(cc.v3(this._viewSize.width, this._ui.node.position.y, 0));
                cc.tween(this._ui.node)
                    .by(0.5, { position: cc.v3(-this._viewSize.width, 0, 0) })
                    .call(() => {
                        cb();
                    })
                    .start();
            }
        ],
        'close': [
            (cb: Function) => {
                cb();
            },
            (cb: Function) => {
                this._ui.node.opacity = 255;
                cc.tween(this._ui.node)
                    .to(0.5, { opacity: 0 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scale = 1;
                cc.tween(this._ui.node)
                    .to(0.5, { scale: 0 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scaleX = 1;
                cc.tween(this._ui.node)
                    .to(0.5, { scaleX: 0 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                this._ui.node.scaleY = 1;
                cc.tween(this._ui.node)
                    .to(0.5, { scaleY: 0 })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                cc.tween(this._ui.node)
                    .by(0.5, { position: cc.v3(0, this._viewSize.height, 0) })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                cc.tween(this._ui.node)
                    .by(0.5, { position: cc.v3(0, -this._viewSize.height, 0) })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                cc.tween(this._ui.node)
                    .by(0.5, { position: cc.v3(-this._viewSize.width, 0, 0) })
                    .call(() => {
                        cb();
                    })
                    .start();
            },
            (cb: Function) => {
                cc.tween(this._ui.node)
                    .by(0.5, { position: cc.v3(this._viewSize.width, 0, 0) })
                    .call(() => {
                        cb();
                    })
                    .start();
            }
        ]
    }

    // 播放入场退场动画
    playAnimation(uiNode: UIBase, openOrClose: string, index: number, cb: Function) {
        //根据openOrClose去调用响应的动画逻辑。
        //动画播放完毕，那么执行回调。
        this._ui = uiNode;
        this._objCallback[openOrClose][index](cb);
    }
}